/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include <algorithm>
#include <cctype>
#include <iostream>

#include "KeyConverter.h"



KeyConverter::KeyConverter(void)
{
}

/** Converts the given key to a string (Ex. MK_BACKSPACE would equal "backspace") */
std::string KeyConverter::ToString( int key ) const
{ 
    std::string k;
    switch(key)
    {
    case MK_BACKSPACE:
        k="BACKSPACE";
        break;
	case MK_TAB:
        k="TAB";
        break;
	case MK_RETURN:
        k="RETURN";
        break;
	case MK_PAUSE:
        k="PAUSE";
        break;
	case MK_ESCAPE:
        k="ESC";
        break;
	case MK_SPACE:
        k="SPACE";
        break;
	case MK_EXCLAIM:
        k="EXCLAIMATION";
        break;
	case MK_QUOTEDBL:
        k="DOUBLEQUOTE";
        break;
	case MK_HASH:
        k="POUNDSIGN";
        break;
	case MK_DOLLAR:
        k="DOLLAR";
        break;
	case MK_AMPERSAND:
        k="AMPERSAND";
        break;
	case MK_QUOTE:
        k="QUOTE";
        break;
	case MK_LEFTPAREN:
        k="LEFTPAREN";
        break;
	case MK_RIGHTPAREN:
        k="RIGHTPAREN";
        break;
	case MK_ASTERISK:
        k="ASTERISK";
        break;
	case MK_PLUS:
        k="PLUS";
        break;
	case MK_COMMA:
        k="COMMA";
        break;
	case MK_MINUS:
        k="MINUS";
        break;
	case MK_PERIOD:
        k="PERIOD";
        break;
	case MK_SLASH:
        k="SLASH";
        break;
    case MK_COLON:
        k="COLON";
        break;
	case MK_SEMICOLON:  k="SEMICOLON";
        break;
	case MK_LESS:       k="LESS";
        break;
	case MK_EQUALS:     k="EQUALS";
        break;  
	case MK_GREATER:    k="GREATER";
        break;
	case MK_QUESTION:   k="QUESTION";
        break;
	case MK_AT: k="AT";
        break;
    case MK_LEFTBRACKET:    k="LEFTBRACKET";
        break;
	case MK_BACKSLASH:k="BACKSLASH";
        break;
	case MK_RIGHTBRACKET:   k="RIGHTBRACKET";
        break;
	case MK_CARET:k="CARET";
        break;
	case MK_UNDERSCORE: k="UNDERSCORE";
        break;
	case MK_BACKQUOTE:  k="BACKQUOTE";
        break;
    case MK_DELETE: k="DELETE";
        break;
    case MK_UP:
        k="UP";
        break;
	case MK_DOWN:
        k="DOWN";
        break;
	case MK_RIGHT:
        k="RIGHT";
        break;
	case MK_LEFT:
        k="LEFT";
        break;
	case MK_INSERT: k="INSERT";
        break;
	case MK_HOME:   k="HOME";
        break;
	case MK_END:    k="END";
        break;
	case MK_PAGEUP: k="PAGEUP";
        break;
	case MK_PAGEDOWN:k="PAGEDOWN";
        break;
    case MK_F1: k="F1";
        break;
	case MK_F2: k="F2";
        break;
	case MK_F3: k="F3";
        break;			
	case MK_F4: k="F4";
        break;			
	case MK_F5: k="F5";
        break;			
	case MK_F6: k="F6";
        break;			
	case MK_F7: k="F7";
        break;			
	case MK_F8: k="F8";
        break;			
	case MK_F9: k="F9";
        break;			
	case MK_F10:    k="F10";
        break;		
	case MK_F11:    k="F11";
        break;		
	case MK_F12:    k="F12";
        break;		
	case MK_F13:
        break;		
	case MK_F14:
        break;		
	case MK_F15:
        break;		
    case MK_NUMLOCK:    k="NUMLOCK";
        break;		
	case MK_CAPSLOCK:   k="CAPSLOCK";
        break;		
	case MK_SCROLLOCK:  k="SCROLLOCK";
        break;		
	case MK_RSHIFT:
        k="RSHIFT";
        break;		
	case MK_LSHIFT:
        k="LSHIFT";
        break;		
	case MK_RCTRL:
        k="RCTRL";
        break;
	case MK_LCTRL:
        k="LCTRL";
        break;		
	case MK_RALT:
        k="RALT";
        break;		
	case MK_LALT:
        k="LALT";
        break;		
	case MK_RMETA:
        break;		
	case MK_LMETA:
        break;		
	case MK_LSUPER:
        break;		/* Left "Windows" key */
	case MK_RSUPER:
        break;				/* Right "Windows" key */
	case MK_MODE:
        break;		
    case MK_HELP:
        break;		
	case MK_PRINT:  k="PRINT";
        break;		
	case MK_SYSREQ:
        break;		
	case MK_BREAK:  k="BREAK";
        break;	
	case MK_MENU:
        break;		
	case MK_POWER:
        break;				/* Power Macintosh power key */
	case MK_UNDO:
        break;
    default:
        k = (char)(key);
        break;
    };
    return k; 
}


/** Convert the given key code to a string */
std::string KeyConverter::JoystickToString( int key ) const
{
    std::string k;
    switch( key )
    {
    case J_A:
        k = "J_A"; break;
    case J_B:
        k = "J_B"; break;
    case J_X:
        k = "J_X"; break;
    case J_Y:
        k = "J_Y"; break;
    case J_LEFT_TRIGGER:
        k = "J_LEFTTRIGGER"; break;
    case J_RIGHT_TRIGGER:
        k = "J_RIGHTTRIGGER"; break;
    case J_BACK:
        k = "J_BACK"; break;
    case J_START:
        k = "J_START"; break;
    case J_LEFT_JOYSTICK:
        k = "J_LEFTJOYSTICK"; break;
    case J_RIGHT_JOYSTICK:
        k = "J_RIGHTJOYSTICK"; break;
    default:
        k = "UNKNOWN";
    };
    return k;
}


// Converts the given string into a valid key code (Ex. "backspace" would equal 8)
int KeyConverter::ToKeyCode( const std::string &keyStr ) const
{ 
    std::string key = keyStr;
    std::transform( key.begin(), key.end(), key.begin(), std::toupper );
    int k=0;
    if ( key == "" )
        return k;

    if ( key == "UP" ) {
        k = MK_UP;
    } else if ( key == "DOWN" ) {
        k = MK_DOWN;
    } else if ( key == "LEFT" ) {
        k = MK_LEFT;
    } else if ( key == "RIGHT" ) {
        k = MK_RIGHT;
    } else if ( key == "LCTRL" ) {
        k = MK_LCTRL;
    } else if ( key == "RCTRL" ) {
        k = MK_RCTRL;
    } else if ( key == "LALT" ) {
        k = MK_LALT;
    } else if ( key == "RALT" ) {
        k = MK_RALT;
    } else if ( key == "LSHIFT" ) {
        k = MK_LSHIFT;
    } else if ( key == "RSHIFT" ) {
        k = MK_RSHIFT;
    } else if ( key == "BACKSPACE" ) {
        k = MK_BACKSPACE;
    } else if ( key == "TAB" ) {
        k = MK_TAB;
    } else if ( key == "RETURN" ) {
        k = MK_RETURN;
    } else if ( key == "ESC" ) {
        k = MK_ESCAPE;
    } else if ( key == "SPACE" ) {
        k = MK_SPACE;
    } else if ( key == "F1" ) {
        k = MK_F1;
    } else if ( key == "F2" ) {
        k = MK_F2;
    } else if ( key == "F3" ) {
        k = MK_F3;
    } else if ( key == "F4" ) {
        k = MK_F4;
    } else if ( key == "F5" ) {
        k = MK_F5;
    } else if ( key == "F6" ) {
        k = MK_F6;
    } else if ( key == "F7" ) {
        k = MK_F7;
    } else if ( key == "F8" ) {
        k = MK_F8;
    } else if ( key == "F9" ) {
        k = MK_F9;
    } else if ( key == "F10" ) {
        k = MK_F10;
    } else if ( key == "F11" ) {
        k = MK_F11;
    } else if ( key == "F12" ) {
        k = MK_F12;
    } else {
        char c = key[0] + 32;
        k = c;
    }
    return k; 
}

/** Convert the given string into a valid Joystick Button */
int KeyConverter::JoystickToKeyCode( std::string &key ) const
{ 
    std::transform( key.begin(), key.end(), key.begin(), std::toupper );
    int k=-1;
    if ( key == "" )
        return k;

    if ( key == "J_A" ) {
        k = J_A;
    } else if ( key == "J_B" ) {
        k = J_B;
    } else if ( key == "J_X" ) {
        k = J_X;
    } else if ( key == "J_Y" ) {
        k = MK_RIGHT;
    } else if ( key == "J_LEFTTRIGGER" ) {
        k = J_LEFT_TRIGGER;
    } else if ( key == "J_RIGHTTRIGGER" ) {
        k = J_RIGHT_TRIGGER;
    } else if ( key == "J_BACK" ) {
        k = J_BACK;
    } else if ( key == "J_START" ) {
        k = J_START;
    } else if ( key == "J_LEFTJOYSTICK" ) {
        k = J_LEFT_JOYSTICK;
    } else if ( key == "J_RIGHTJOYSTICK" ) {
        k = J_RIGHT_JOYSTICK;
    } 
    return k; 
}

// Returns the shift value of the key (Ex. MK_A would return 'A')
char KeyConverter::GetShiftValue( int key ) const
{ 
    int k=0;
    switch(key)
    {
	case MK_COMMA:
        k= MK_LESS;
        break;
	case MK_MINUS:
        k=MK_UNDERSCORE;
        break;
	case MK_PERIOD:
        k= MK_GREATER;
        break;
	case MK_SLASH:
        k= MK_QUESTION;
        break;
	case MK_SEMICOLON:
        k = MK_COLON;
        break;
	case MK_EQUALS:
        k = MK_PLUS;
        break;
    case MK_LEFTBRACKET:
        k = 123; // ascii value
        break;
	case MK_BACKSLASH:
        k = 124;
        break;
	case MK_RIGHTBRACKET:
        k = 125;
        break;
	case MK_BACKQUOTE:
        k = 126;
        break;	
    case MK_0:
        k = MK_RIGHTPAREN;
        break;
    case MK_1:
        k = MK_EXCLAIM;
        break;
    case MK_2:
        k = MK_AT;
        break;
    case MK_3:
        k = MK_HASH;
        break;
    case MK_4:
        k = MK_DOLLAR;
        break;
    case MK_5:
        k = MK_PERCENT;
        break;
    case MK_6:
        k = MK_CARET;
        break;
    case MK_7:
        k = MK_AMPERSAND;
        break;
    case MK_8:
        k = MK_ASTERISK;
        break;
    case MK_QUOTE:
        k = MK_QUOTEDBL;
        break;
    case MK_9:
        k = MK_LEFTPAREN;
        break;
    default:
        if ( key > 31 && key < 127 ) 
            k = key - 32;
        else k = 37;//MK_UNKNOWN;
        break;
    };
    return k; 
}

KeyConverter::~KeyConverter(void)
{
}

/**---------------------------------------------
        Mouse Button Converter
------------------------------------------------ **/


/** Convert a mouse button to a string */
std::string MouseConverter::ToString( int keyCode ) const
{
    std::string keyStr;
    switch( keyCode )
    {
    case MMB_UNKNOWN:   keyStr = "UNKNOWN";
        break;
    case MMB_BUTTON1:   keyStr = "BUTTON1";
        break;
    case MMB_BUTTON2:   keyStr = "BUTTON2";
        break;
    case MMB_BUTTON3:   keyStr = "BUTTON3";
        break;
    case MMB_WHEELUP:   keyStr = "WHEELUP";
        break;
    case MMB_WHEELDOWN: keyStr = "WHEELDOWN";
        break;
    default:
        keyStr = "UNKNOWN";

    };
    return keyStr;
}


/** Convert a string to a key code */
int MouseConverter::ToKeyCode( const std::string &keyStr ) const
{
    std::string key = keyStr;
    std::transform( key.begin(), key.end(), key.begin(), std::toupper );

    if ( key == "" )
        return MMB_UNKNOWN;

    if ( key == "BUTTON1" )
        return MMB_BUTTON1;
    else if ( key == "BUTTON2" )
        return MMB_BUTTON2;
    else if ( key == "BUTTON3" )
        return MMB_BUTTON3;

    else if ( key == "WHEELUP" )
        return MMB_WHEELUP;
    else if ( key == "WHEELDOWN" )
        return MMB_WHEELDOWN;

    return MMB_UNKNOWN;
}